//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
//

package com.aidong.web.controller.pt;

import com.aidong.dao.base.ConditionMap;
import com.aidong.dao.base.OrderSet;
import com.aidong.dao.base.Pagination;
import com.aidong.dao.wills.DicShopCodeDao;
import com.aidong.dao.wills.DicTranCodeDao;
import com.aidong.dao.wills.PtItemCodeDao;
import com.aidong.dao.wills.PtPriceDao;
import com.aidong.model.wills.DicShopCode;
import com.aidong.model.wills.PtItemCode;
import com.aidong.model.wills.PtPrice;
import com.aidong.web.controller.BaseController;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping({"ptItem"})
public class PtItemCodeController extends BaseController {
    @Resource
    PtItemCodeDao ptItemCodeDao;
    @Resource
    DicShopCodeDao dicShopCodeDao;
    @Resource
    PtPriceDao ptPriceDao;
    @Resource
    DicTranCodeDao dicTranCodeDao;

    public PtItemCodeController() {
    }

    @RequestMapping({"list"})
    public ModelAndView list() {
        return (new ModelAndView("pt/ptitem/list")).addObject("itemType", this.getTypeList("ptItemType"));
    }

    @RequestMapping({"_list"})
    public ModelAndView _list(@RequestParam(value = "name",required = false) String name, @RequestParam(value = "itemType",required = false) String itemType, @RequestParam(value = "pageNo",required = false) String pageNo, @RequestParam(value = "totalCount",required = false) String totalCount) {
        Pagination pg = new Pagination(1, 20);
        pg.setPage(this.parseInt(pageNo), this.parseInt(totalCount));
        ConditionMap cd = new ConditionMap();
        cd.eq("itemType", itemType);
        ConditionMap cd1 = new ConditionMap(1);
        if(!this.chkNull(name)) {
            cd1.like("code", "%" + name + "%");
            cd1.like("chName", "%" + name + "%");
            cd.addSub(cd1);
        }

        this.ptItemCodeDao.queryPageList(pg, cd, (new OrderSet()).asc("code"));
        ModelAndView mv = new ModelAndView("pt/ptitem/_list");
        mv.addObject("list", pg.getResultList());
        mv.addObject("pg", pg);
        return mv;
    }

    @RequestMapping({"add"})
    public ModelAndView add(@RequestParam(value = "code",required = false) String code) {
        ModelAndView mv = new ModelAndView("pt/ptitem/edit");
        PtItemCode item = null;
        List shopList;
        if(!this.chkNull(code)) {
            item = (PtItemCode)this.ptItemCodeDao.getById(code);
            if(item != null) {
                shopList = this.ptItemCodeDao.getPtShopList(code, true);
                List i$ = this.ptPriceDao.getPtPriceList(code);
                item.setCode((String)null);
                Iterator shop = i$.iterator();

                while(shop.hasNext()) {
                    PtPrice price = (PtPrice)shop.next();
                    price.setAutoId((Integer)null);
                }

                mv.addObject("shopList", shopList);
                mv.addObject("priceList", i$);
            }
        }

        if(item == null) {
            item = new PtItemCode();
            item.setStatus(Integer.valueOf(1));
            shopList = this.dicShopCodeDao.getAllShopList();
            Iterator i$1 = shopList.iterator();

            while(i$1.hasNext()) {
                DicShopCode shop1 = (DicShopCode)i$1.next();
                shop1.setStatus(Integer.valueOf(0));
            }

            mv.addObject("shopList", shopList);
        }

        mv.addObject("item", item);
        mv.addObject("itemType", this.getTypeList("ptItemType"));
        mv.addObject("coachLevel", this.getTypeList("coachLevel"));
        mv.addObject("tranCode", this.dicTranCodeDao.getListByCategory("02"));
        mv.addObject("_oper", "add");
        return mv;
    }

    @RequestMapping({"_add"})
    @ResponseBody
    public String _add(@ModelAttribute("item") PtItemCode item, @RequestParam(value = "shops",required = false) String shops, @RequestParam(value = "prices",required = false) String prices) {
        if(this.chkNull(item.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(item.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(item.getItemType())) {
            return this.getResult(false, "请选择课程类型");
        } else {
            if(this.chkNullZ(item.getStatus())) {
                item.setStatus(Integer.valueOf(1));
            }

            if(this.chkNullZ(item.getMonster())) {
                item.setMonster(Integer.valueOf(0));
            }

            if(this.ptItemCodeDao.exists(item.getCode())) {
                return this.getResult(false, "编号已存在");
            } else {
                item.setValids(Integer.valueOf(0));
                item.setValidUnit("01");
                item.setCreateUser(this.getLoginUserCode());
                item.setCreateTime(new Date());
                item.setUpdateTime(new Date());
                ArrayList shopList = new ArrayList();
                if(!this.chkNull(shops)) {
                    String[] priceList = shops.split(",");
                    String[] e = priceList;
                    int len$ = priceList.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String s = e[i$];
                        if(!this.chkNull(s)) {
                            shopList.add(s.trim());
                        }
                    }
                }

                List var11 = this.getPtPrice(prices);
                if(var11 == null) {
                    return this.getResult(false, "数据错误");
                } else {
                    try {
                        this.ptItemCodeDao.addPtItem(item, shopList, var11);
                        this.reloadTypeList("ptItemName");
                        this.addUserLog("新增私教课程", "编号:" + item.getCode() + ",名称:" + item.getChName());
                    } catch (Exception var10) {
                        this.log.error("add ptitem", var10);
                        return this.getResult(false, "保存发生错误");
                    }

                    return this.getResult(true, "保存完成");
                }
            }
        }
    }

    private List<PtPrice> getPtPrice(String prices) {
        ArrayList list = new ArrayList();
        if(!this.chkNull(prices)) {
            String[] ss = prices.split("\\|");
            String[] arr$ = ss;
            int len$ = ss.length;

            for(int i$ = 0; i$ < len$; ++i$) {
                String s1 = arr$[i$];
                String[] s = s1.split(",");
                Integer id = this.parseInt(s[0]);
                PtPrice price;
                if(s.length == 2 && "-".equals(s[1]) && !this.chkNullZ(id)) {
                    price = new PtPrice(id);
                    price.setPtCode("-");
                } else {
                    if(s.length <= 3) {
                        return null;
                    }

                    price = new PtPrice(id);
                    price.setCoachLevel(s[1]);
                    price.setCountMin(this.parseInt(s[2]));
                    price.setCountMax(this.parseInt(s[3]));
                    price.setPriceMin(this.parseDouble(s[4]));
                    price.setPriceMax(this.parseDouble(s[5]));
                    price.setValidDays(this.parseInt(s[6]));
                }

                list.add(price);
            }
        }

        return list;
    }

    @RequestMapping({"edit"})
    public ModelAndView edit(@RequestParam("code") String code) {
        PtItemCode item = (PtItemCode)this.ptItemCodeDao.getById(code);
        if(item == null) {
            return this.showAlert("没有这个记录");
        } else {
            List shopList = this.ptItemCodeDao.getPtShopList(code, true);
            List priceList = this.ptPriceDao.getPtPriceList(code);
            ModelAndView mv = new ModelAndView("pt/ptitem/edit");
            mv.addObject("item", item);
            mv.addObject("shopList", shopList);
            mv.addObject("priceList", priceList);
            mv.addObject("itemType", this.getTypeList("ptItemType"));
            mv.addObject("coachLevel", this.getTypeList("coachLevel"));
            mv.addObject("tranCode", this.dicTranCodeDao.getListByCategory("02"));
            mv.addObject("_oper", "edit");
            return mv;
        }
    }

    @RequestMapping({"_edit"})
    @ResponseBody
    public String _edit(@ModelAttribute("item") PtItemCode item, @RequestParam(value = "shops",required = false) String shops, @RequestParam(value = "prices",required = false) String prices) {
        if(this.chkNull(item.getCode())) {
            return this.getResult(false, "请输入编号");
        } else if(this.chkNull(item.getChName())) {
            return this.getResult(false, "请输入名称");
        } else if(this.chkNull(item.getItemType())) {
            return this.getResult(false, "请选择课程类型");
        } else {
            if(this.chkNullZ(item.getStatus())) {
                item.setStatus(Integer.valueOf(1));
            }

            if(this.chkNullZ(item.getMonster())) {
                item.setMonster(Integer.valueOf(0));
            }

            PtItemCode item1 = (PtItemCode)this.ptItemCodeDao.getById(item.getCode());
            if(item1 == null) {
                return this.getResult(false, "没有这个记录");
            } else {
                item1.setChName(item.getChName());
                item1.setActiveTime(item.getActiveTime());
                item1.setInactiveTime(item.getInactiveTime());
                item1.setItemType(item.getItemType());
                item1.setTraCode(item.getTraCode());
                item1.setNote(item.getNote());
                item1.setStatus(item.getStatus());
                item1.setMonster(item.getMonster());
                item1.setUpdateTime(new Date());
                ArrayList shopList = new ArrayList();
                if(!this.chkNull(shops)) {
                    String[] priceList = shops.split(",");
                    String[] e = priceList;
                    int len$ = priceList.length;

                    for(int i$ = 0; i$ < len$; ++i$) {
                        String s = e[i$];
                        if(!this.chkNull(s)) {
                            shopList.add(s.trim());
                        }
                    }
                }

                List var12 = this.getPtPrice(prices);
                if(var12 == null) {
                    return this.getResult(false, "数据错误");
                } else {
                    try {
                        this.ptItemCodeDao.updatePtItem(item1, shopList, var12);
                        this.reloadTypeList("ptItemName");
                        this.addUserLog("修改私教课程", "编号:" + item.getCode() + ",名称:" + item.getChName());
                    } catch (Exception var11) {
                        this.log.error("edit ptitem", var11);
                        return this.getResult(false, "保存发生错误");
                    }

                    return this.getResult(true, "保存完成");
                }
            }
        }
    }

    @RequestMapping({"_del"})
    @ResponseBody
    public String _del(@RequestParam("code") String code) {
        PtItemCode item = (PtItemCode)this.ptItemCodeDao.getById(code);
        if(item == null) {
            return this.getResult(false, "没有这个记录");
        } else {
            try {
                this.ptItemCodeDao.updateStatus(code, 0);
                this.reloadTypeList("ptItemName");
                this.addUserLog("删除私教课程", "编号:" + item.getCode() + ",名称:" + item.getChName());
            } catch (Exception var4) {
                this.log.error("del ptitem", var4);
                return this.getResult(false, "删除发生错误");
            }

            return this.getResult(true, "删除成功");
        }
    }
}
